/**
 * 
 */
package com.csc.bankingsystem.core.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.csc.bankingsystem.core.dao.TokenRecordDAO;
import com.csc.bankingsystem.core.entity.TokenRecord;
import com.csc.bankingsystem.core.entity.TransferRecord;
import com.csc.bankingsystem.core.entity.UserInfo;

/**
 * Define all Data access operations of TokenRecordDAO
 * 
 * @author angelkid
 *
 */
@Repository("tokenRecordDAO")
public class TokenRecordDAOImpl extends GenericDAOImpl<TokenRecord> implements
		TokenRecordDAO {

	public TokenRecord findFirstByTokenAndUserInfoId(String tokenMD5, long userInfoId) {
		Criteria criteria = getSession().createCriteria(TokenRecord.class);
		criteria.add(Restrictions.eq("tokenMD5", tokenMD5));
		criteria.add(Restrictions.eq("userInfo.id", userInfoId));
		@SuppressWarnings("unchecked")
		List<TokenRecord> list = criteria.list();
		return (CollectionUtils.isEmpty(list))?null:list.get(0);
	}
	
	public List<TokenRecord> findByUserInfoId(long userInfoId){
		Criteria criteria = getSession().createCriteria(TokenRecord.class);
		criteria.add(Restrictions.eq("userInfo.id", userInfoId));
		@SuppressWarnings("unchecked")
		List<TokenRecord> list = criteria.list();
		return (CollectionUtils.isEmpty(list))?null:list;
	}
	
	public void delete(List<TokenRecord> listTokenRecord) {
		
		for (TokenRecord tokenRecord : listTokenRecord) {
			delete(tokenRecord);
		}
	}
}
